package com.ym.b2b.dictionary.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.commons.log.CusLog;
import com.ym.b2b.dictionary.service.EmSDictionaryService;
import com.ym.b2b.dictionary.sqlbean.EmSDictionary;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;
import com.zc.export.ExcelUtil;

@Controller
@RequestMapping("/dictionary")
public class ManagerDictionaryController extends BaseController {

    @Autowired
    private EmSDictionaryService emSDictionaryService;

    // 列表查询
    @RequestMapping("searchPageDictionary.do")
    @ResponseBody
    public Result searchPageDictionary(EmSDictionary emSDictionary, Page<EmSDictionary> page, HttpServletRequest req) {
        Result result = new Result();
        result = emSDictionaryService.searchPageEmSDictionary(emSDictionary, page);
        return result;
    }

    // 导出
    @RequestMapping(value = "searchPageDictionaryExcel.do")
    @ResponseBody
    public Result searchPageDictionaryExcel(EmSDictionary emSDictionary, Page<EmSDictionary> page, String loadType) {
        Result ret = new Result();
        try {
            if (loadType == null || loadType.equals("")) {
                ret.setCode(Result.CODE_100);
                ret.setResult("loadType 参数 '数量'  不能为空!");
                return ret;
            }
            page.setPageable("true");
            page.setPageSize(Integer.parseInt(loadType));
            page.setStartRow(1);
            ret = emSDictionaryService.searchPageEmSDictionary(emSDictionary, page);
            ExcelUtil<EmSDictionary> excelUtil = new ExcelUtil<EmSDictionary>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", ret.getRows());
            ret = excelUtil.createExcel("DictionaryListExcel", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }

    // 新增记录
    @RequestMapping("insertDictionary.do")
    @ResponseBody
    public Result insertDictionary(EmSDictionary emSDictionary, HttpServletRequest req) {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 新增站点资料  ", "USER", 2);
        Result result = new Result();
        result = emSDictionaryService.insertEmSDictionary(emSDictionary);
        return result;
    }

    // 获取单条记录内容
    @RequestMapping("searchEmSDictionary.do")
    @ResponseBody
    public Result searchEmSDictionary(EmSDictionary emSDictionary, HttpServletRequest req) {
    	Result result = new Result();
        try {
        	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 查询站点资料: " + JSON.toJSONString(emSDictionary), "USER", 2);
            result = emSDictionaryService.searchEmSDictionary(emSDictionary);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    // 删除记录内容
    @RequestMapping("deleteEmSDictionary.do")
    @ResponseBody
    public Result deleteEmSDictionary(EmSDictionary emSDictionary, HttpServletRequest req) {
    	CusLog.writeLog("******" + getManagerTokenUser(req).getUserName() + " 删除站点资料: " + JSON.toJSONString(emSDictionary), "USER", 2);
        Result result = new Result();
        try {
            emSDictionaryService.deleteEmSDictionary(emSDictionary.getDictionaryId());
        } catch (Exception e) {
            // TODO: handle exception
            result.setCode("100");
            return result;
        }
        result.setCode("200");
        return result;
    }

    // 删除多条记录内容
    @RequestMapping("deleteMultipleEmSDictionary.do")
    @ResponseBody
    public Result deleteMultipleEmSDictionary(String dictionaryIdList, HttpServletRequest req) {
    	CusLog.writeLog("******" + getManagerTokenUser(req).getUserName() + " 批量删除站点资料: " + dictionaryIdList, "USER", 2);
        Result result = new Result();
        try {
            emSDictionaryService.deleteMultipleEmSDictionary(dictionaryIdList);
        } catch (Exception e) {
            // TODO: handle exception
            result.setCode("100");
            return result;
        }
        result.setCode("200");
        return result;
    }

    // 更新记录
    @RequestMapping("updateEmSDictionary.do")
    @ResponseBody
    public Result updateEmSDictionary(EmSDictionary emSDictionary, HttpServletRequest req) throws DataAccessException {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 更新站点资料: " + JSON.toJSONString(emSDictionary), "USER", 2);

        Result result = new Result();
        result = emSDictionaryService.updateEmSDictionary(emSDictionary);
        return result;
    }

    // 批量更新记录
    @RequestMapping("updateEmSDictionaryBat.do")
    @ResponseBody
    public Result updateEmSDictionaryBat(EmSDictionary emSDictionary, HttpServletRequest req) throws DataAccessException {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 批量更新站点资料: " + JSON.toJSONString(emSDictionary), "USER", 2);
        Result result = new Result();
        result = emSDictionaryService.updateEmSDictionaryBat(emSDictionary);
        return result;
    }

}
